(function(w,factory){
	if(w){
		w.dsCommon = factory(w);
	}else{
		w.dsCommon = {};
	}
})(
	typeof window!="undefined"?window:undefined,
	function(w){
		//getTreeData
		var _tdSettings = {
			initId:"0",
			pId:"parent_id",
			cId:"id"
		};
		//
		return {
			getPageBtns:function(opt){ //分页页码
				var options = opt || {},
					btnCount = opt.btnCount || 7,
					pageCount = opt.pageCount || 1,
					current = opt.current || 1;
				if(btnCount > pageCount ){
					btnCount = pageCount;
				}
				if(current > pageCount){
					current = pageCount;
				}
				var _offset = (btnCount - 1) / 2,
					offset = {
						start:current - _offset,
						end:current + _offset
					};
				if(offset.start < 1){
					offset.end = offset.end + (1 - offset.start);
					offset.start = 1;
				}
				if(offset.end > pageCount){
					offset.start = offset.start - (offset.end - pageCount);
					offset.end = pageCount;
				}
				var array = [];
				for(var i=offset.start;i<=offset.end;i++){
					array.push(i);
				}
				return array;	
			},
			throttle:function(fn,interval){ //节流
				var _self = fn,
					timer,
					firstTime = true;
				return function(){
					var args = arguments,
						__me = this;
					if(firstTime){
						__self.apply(__me,args);
						return firstTime = false;
					}
					if(timer){
						return false;
					}
					timer = setTimeout(function(){
						clearTimeout(timer);
						timer = null;
						__self.apply(__me,args);
					},interval||200);
				};
			},
			getTreeData:function(list,opt){ //treeData1
				var options = opt || {};
				options.initId = options.initId || _tdSettings.initId;
				options.pId = options.pId || _tdSettings.pId;
				options.cId = options.cId || _tdSettings.cId;
				var items = {};
				for(var i = 0,len=list.length;i<len;i++){
					var listItem = list[i],
						key = listItem[options.pId];
					if(items[key]){
						items[key].push(listItem);
					}else{
						items[key] = [];
						items[key].push(listItem);
					}
				}
				function formatData(items,parentId){
					var result = [];
					if(!items[parentId]){
						return result;
					}
					for(var i = 0;i<items[parentId].length;i++){
						var t = items[parentId][i];
						t.children = formatData(items,t[options.cId]);
						result.push(t);
					}
					return result;
				}
				return formatData(items,options.initId);
			},
			quickSort:function(arr){ //
				if(arr.length<=1){ 
					return arr;
				}
				var pi = ~~(arr.length / 2 ),
					piEle = arr.splice(pi,1)[0],
					left = [],
					right = [];
				for(var i=0,len=arr.length;i<len;i++){
					if(arr[i]<piEle){
						left.push(arr[i]);
					}else{
						right.push(arr[i]);
					}
				}
				return this.quickSort(left).concat(piEle,this.quickSort(right));
			},
			weekOfMonth:function(y,m,d){ //指定日期在月份内是第几周
				var oDate = new Date(y,parseInt(m)-1,d),
					w = oDate.getDay(),
					d = oDate.getDate();
				if(w===0){
					w = 7;
				}
				return {
					getYear:oDate.getFullYear(),
					getMonth:oDate.getMonth()-1,
					getWeek:Math.ceil((d+6-w)/7)
				}; 
			},
			j2h:function(jd){ //
				return jd?(Math.PI/180)*jd:"";
			},
			h2j:function(hd){ //
				return hd?(180/Math.PI)*hd:"";
			}
		};
	}
);
//
export default window.dsCommon;
